﻿using FluentResourceMapping.AutoMapping;
using FluentResourceMapping.Tests.TestDummies;
using NUnit.Framework;

// ReSharper disable InconsistentNaming

namespace FluentResourceMapping.Tests
{
    [TestFixture]
    public class AutoResourceMappingTests
    {
        private ResourceMappingModel model;

        [SetUp]
        public void Setup()
        {
            model = Fluently.Configure()
                .Mappings(o => o.AutoResourceMappings.Add(AutoMap.AssemblyOf<AutoMappedClassResource>()))
                .BuildResourceMappingModel();
        }

        [Test]
        public void Can_Find_Mapping()
        {
            // Act
            var target = model.FindMapping(typeof (AutoMappedClass));

            // Assert
            Assert.IsNotNull(target);
        }

        [Test]
        public void Can_Get_Resource()
        {
            // Arrange
            TypeResourceMapping mapping = model.FindMapping(typeof (AutoMappedClass));

            // Act
            string resultResource = mapping.GetResource("Manufacturer");

            // Assert
            Assert.AreEqual("Fabricante", resultResource);
        }

        [Test]
        public void GetResource_Returns_Mapping_For_Classes_Without_Resource()
        {
            // Act
            TypeResourceMapping mapping = model.FindMapping(typeof (ClassWithoutResource));

            // Assert
            Assert.IsNotNull(mapping);
            Assert.AreEqual(0, mapping.MemberResourceMappings.Count);
        }

        [Test]
        public void GetResource_Returns_Null_For_Non_Mapped_Property()
        {
            // Arrange
            TypeResourceMapping mapping = model.FindMapping(typeof (AutoMappedClass));

            // Act
            string resultResource = mapping.GetResource("NonMappedProperty");

            // Assert
            Assert.IsNull(resultResource);
        }

        [Test]
        public void Can_Override_AutoMapping()

        {
            // Act
            ResourceMappingModel otherModel = Fluently.Configure()
                .Mappings(o => o.AutoResourceMappings.Add(AutoMap.AssemblyOf<AutoMappedClassResource>()
                                                              .UseOverridesFromAssemblyOf<AutoResourceMappingTests>))
                .BuildResourceMappingModel();

            var result = otherModel.FindMapping((typeof (AutoMappedClass)));

            // Assert      
            Assert.AreEqual("Fabricante", result.GetResource("Manufacturer"));
            Assert.AreEqual("Chambres", result.GetResource("Rooms"));
        }

        [Test]
        public void Can_Override_Convention()
        {
            // Act
            ResourceMappingModel otherModel = Fluently.Configure()
                .Mappings(o => o.AutoResourceMappings.Add(AutoMap.AssemblyOf<AutoMappedClassResource>()
                                                              .Convention(new TestsConvention())))
                .BuildResourceMappingModel();

            var result = otherModel.FindMapping((typeof (AutoMappedClass)));

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual("Fabricant", result.GetResource("Manufacturer"));
            Assert.AreEqual("Chambres", result.GetResource("Rooms"));
        }

        [Test]
        public void Can_Override_Automapping_Configuration()
        {
            // Act
            var otherModel = Fluently.Configure()
                .Mappings(
                    o =>
                    o.AutoResourceMappings.Add(AutoMap.AssemblyOf<AutoMappedClassResource>(
                        new AutomappingConfigurationOverride())
                                                   .Convention(new SameMemberNameResourceMappingConvention())))
                .BuildResourceMappingModel();

            var result = otherModel.FindMapping(typeof (ClassSameNameAsResource));

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual("Descricao", result.GetResource("Description"));
        }
    }
}

// ReSharper restore InconsistentNaming